package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.ButtongroupService;
import com.cyz.admin.vo.ButtongroupVo;
import com.cyz.common.bean.Buttongroup;
import com.cyz.common.dto.ButtonGroupDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.ButtonMapper;
import com.cyz.common.mapper.ButtongroupMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 管理端免费课程按钮分类接口实现类
 */
@Service
@Transactional
public class ButtongroupServiceImpl extends ServiceImpl<ButtongroupMapper, Buttongroup>
    implements ButtongroupService{

    @Autowired
    private ButtongroupMapper buttongroupMapper;
    @Autowired
    private ButtonMapper buttonMapper;

    @Override
    public R queryFreeButtonGroups() {
//        LambdaQueryWrapper<Buttongroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //根据名字进行排序
//        lambdaQueryWrapper.orderByAsc(Buttongroup::getName);
//        //查询数据
//        List<Buttongroup> list = list(lambdaQueryWrapper);

        //查询数据
        //根据名字进行排序
        List<Buttongroup> list = buttongroupMapper.queryFreeButtonGroups();

        return R.success(list);
    }

    @Override
    public R queryFreeButtonGroupsAndButtons() {
//        LambdaQueryWrapper<Buttongroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //根据名字进行排序
//        lambdaQueryWrapper.orderByAsc(Buttongroup::getName);
//        //查询数据
//        List<Buttongroup> list = list(lambdaQueryWrapper);

        //根据名字进行排序
        //查询数据
        List<Buttongroup> list = buttongroupMapper.queryFreeButtonGroups();

        //vo转化
        List<ButtongroupVo> buttongroupVos = BeanCopyUtil.copyBeanList(list, ButtongroupVo.class);

        //查询各分组里面的按钮
        buttongroupVos
                .forEach(item -> item.setButtons(buttonMapper.getButtonsByGroupId(item.getId())));

        //错误判断
        if(buttongroupVos.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(buttongroupVos);
    }

    @Override
    public R deleteFreeButtonGroups(Integer[] ids) {
//        //删除该分类
//        boolean flag = removeByIds(Arrays.asList(ids));

        //删除分类
        boolean flag = buttongroupMapper.removeByIds(ids);

        //错误判断
        if(flag){
            return queryFreeButtonGroups();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }

    @Override
    public R addFreeButtonGroup(ButtonGroupDto buttonGroupDto) {
        //dto转化
        Buttongroup buttongroup = BeanCopyUtil.copyBean(buttonGroupDto, Buttongroup.class);
//        //添加数据
//        boolean flag = save(buttongroup);
        //添加数据
        boolean flag = buttongroupMapper.addFreeButtonGroup(buttongroup);

        //错误判断
        if(flag){
            return queryFreeButtonGroups();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R updateFreeButtonGroup(ButtonGroupDto buttonGroupDto) {
        //dto转化
        Buttongroup buttongroup = BeanCopyUtil.copyBean(buttonGroupDto, Buttongroup.class);
        System.out.println(buttongroup);
        //更新数据
        boolean flag = buttongroupMapper.updateFreeButtonGroup(buttongroup);
        //错误判断
        if(flag){
            return queryFreeButtonGroups();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }
}




